package com.zx.BookSeaReader.greendao.service;

import android.util.Size;

import com.zx.BookSeaReader.greendao.GreenDaoManager;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookChapterComment;
import com.zx.BookSeaReader.greendao.entity.BookCommentBean;
import com.zx.BookSeaReader.greendao.entity.BookListBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.gen.BookChapterCommentDao;
import com.zx.BookSeaReader.greendao.gen.BookCommentBeanDao;
import com.zx.BookSeaReader.util.Constant;
import com.zx.BookSeaReader.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ZX
 * @Time: 2020/3/10
 * @Description: 评论
 */
public class CommentService extends BaseService {
    /**
     * @Author: ZX
     * @Time: 2020/3/28
     * @Description: 添加评论
     */
    public void addComment(Book book, UserBean userBean, String content, int size){
        BookCommentBean bookCommentBean = new BookCommentBean();
        bookCommentBean.setBookId(book.getId());
        bookCommentBean.setContent(content);
        bookCommentBean.setLikeCount(0);
        bookCommentBean.setCreateTime(StringUtils.
                dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
        bookCommentBean.setUsername(userBean.getUsername());
        bookCommentBean.setFloorCount(size);
        addEntity(bookCommentBean);
    }
    /**
     * @Author: ZX
     * @Time: 2020/3/28
     * @Description: 添加章节评论
     */
    public void addChapterComment(Book book, UserBean userBean, String content, int size,String chapterName){
        BookChapterComment bookChapterComment = new BookChapterComment();
        bookChapterComment.setBookId(book.getId());
        bookChapterComment.setContent(content);
        bookChapterComment.setLikeCount(0);

        bookChapterComment.setChapterNum(book.getHisttoryChapterNum());
        bookChapterComment.setReadPosition(book.getLastReadPosition());
        bookChapterComment.setChapterName(chapterName);

        bookChapterComment.setCreateTime(StringUtils.
                dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
        bookChapterComment.setUsername(userBean.getUsername());
        bookChapterComment.setFloorCount(size);
        addEntity(bookChapterComment);
    }

    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书章节评论
     */
    public List<BookChapterComment> getBookChapterComments(Book book){
        BookChapterCommentDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookChapterCommentDao();
        List<BookChapterComment> bookChapterComments=new ArrayList<>();
        bookChapterComments=bookCommentBeanDao.queryBuilder()
                .where(BookChapterCommentDao.Properties.BookId.eq(book.getId()),BookChapterCommentDao.Properties.ChapterNum.eq(book.getHisttoryChapterNum()))
                .orderDesc(BookChapterCommentDao.Properties.CreateTime)
                .list();
        if (bookChapterComments.size() > 0) {
            return bookChapterComments;
        } else {
            bookChapterComments=new ArrayList<>();
        }
        return bookChapterComments;

    }

    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书章节热门评论
     */
    public List<BookChapterComment> getBookChapterHotComments(Book book){
        BookChapterCommentDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookChapterCommentDao();
        List<BookChapterComment> bookChapterComments=new ArrayList<>();
        List<BookChapterComment> hotCommentBeans=new ArrayList<>();
        bookChapterComments=bookCommentBeanDao.queryBuilder()
                .where(BookChapterCommentDao.Properties.BookId.eq(book.getId()),BookChapterCommentDao.Properties.ChapterNum.eq(book.getHisttoryChapterNum()))
                .orderDesc(BookChapterCommentDao.Properties.LikeCount)
                .list();
        if (bookChapterComments.size() > 0) {
            hotCommentBeans.add(bookChapterComments.get(0));
        }
        if(bookChapterComments.size() > 1){
            hotCommentBeans.add(bookChapterComments.get(1));
        }
//        if(commentBeans.size() > 2){
//            hotCommentBeans.add(commentBeans.get(2));
//        }
        return hotCommentBeans;

    }

    /**
     * @Author: ZX
     * @Time: 2020/3/28
     * @Description: 添加书单评论
     */
    public void addBookListComment(BookListBean bookListBean, UserBean userBean, String content, int size){
        BookCommentBean bookCommentBean = new BookCommentBean();
        bookCommentBean.setListId(bookListBean.getListId());
        bookCommentBean.setContent(content);
        bookCommentBean.setLikeCount(0);
        bookCommentBean.setCreateTime(StringUtils.
                dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
        bookCommentBean.setUsername(userBean.getUsername());
        bookCommentBean.setFloorCount(size);
        addEntity(bookCommentBean);
    }
    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书评论
     */
    public List<BookCommentBean> getBookComment(Book book){
        BookCommentBeanDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookCommentBeanDao();
        List<BookCommentBean> commentBeans=new ArrayList<>();
        commentBeans=bookCommentBeanDao.queryBuilder()
                .where(BookCommentBeanDao.Properties.BookId.eq(book.getId()))
                .orderDesc(BookCommentBeanDao.Properties.CreateTime)
                .list();
        if (commentBeans.size() > 0) {
            return commentBeans;
        } else {
            commentBeans=new ArrayList<>();
        }
        return commentBeans;

    }
    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书单评论
     */
    public List<BookCommentBean> getBookListComments(BookListBean bookListBean){
        BookCommentBeanDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookCommentBeanDao();
        List<BookCommentBean> commentBeans=new ArrayList<>();
        commentBeans=bookCommentBeanDao.queryBuilder()
                .where(BookCommentBeanDao.Properties.ListId.eq(bookListBean.getListId()))
                .orderDesc(BookCommentBeanDao.Properties.CreateTime)
                .list();
        if (commentBeans.size() > 0) {
            return commentBeans;
        } else {
            commentBeans=new ArrayList<>();
        }
        return commentBeans;

    }

    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书单热门评论
     */
    public List<BookCommentBean> getBookListHotComments(BookListBean bookListBean){
        BookCommentBeanDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookCommentBeanDao();
        List<BookCommentBean> commentBeans=new ArrayList<>();
        List<BookCommentBean> hotCommentBeans=new ArrayList<>();
        commentBeans=bookCommentBeanDao.queryBuilder()
                .where(BookCommentBeanDao.Properties.ListId.eq(bookListBean.getListId()))
                .orderDesc(BookCommentBeanDao.Properties.LikeCount)
                .list();
        if (commentBeans.size() > 0) {
            hotCommentBeans.add(commentBeans.get(0));
        }
        if(commentBeans.size() > 1){
            hotCommentBeans.add(commentBeans.get(1));
        }
//        if(commentBeans.size() > 2){
//            hotCommentBeans.add(commentBeans.get(2));
//        }
        return hotCommentBeans;

    }


    /**
     * @Author: ZX
     * @Time: 2020/3/15
     * @Description: 查找该书热门评论
     */
    public BookCommentBean getBookHotComment(Book book){
        BookCommentBeanDao bookCommentBeanDao = GreenDaoManager.getInstance().getSession().getBookCommentBeanDao();
        List<BookCommentBean> commentBeans=new ArrayList<>();
        commentBeans=bookCommentBeanDao.queryBuilder()
                .where(BookCommentBeanDao.Properties.BookId.eq(book.getId()))
                .orderDesc(BookCommentBeanDao.Properties.LikeCount)
                .list();
        if (commentBeans.size() > 0) {
            return commentBeans.get(0);
        } else {
            return null;
        }

    }
}
